/*
 * Copyright (C) 2012  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.remote.rsa.message.transport;

import nemos.remote.rsa.message.data.Message;

/**
 * LocalTransport : The local transport implementation.
 * 
 * @author Romain DUBOIS
 */
public class LocalTransport extends AbstractTransport {

    /** The peer transport */
    private final LocalTransport mPeer;

    /**
     * Constructor.
     * 
     * @param pId
     *            The transport ID
     */
    public LocalTransport(final String pId) {
        super(pId);

        mPeer = new LocalTransport(pId, this);
    }

    /**
     * Constructor.
     * 
     * @param pId
     *            The transport ID
     * @param pPeer
     *            The peer transport
     */
    public LocalTransport(final String pId, final LocalTransport pPeer) {
        super(pId);

        mPeer = pPeer;
    }

    /**
     * Get the peer transport.
     * 
     * @return Tpeer transport
     */
    public LocalTransport getPeer() {
        return mPeer;
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.remote.rsa.message.transport.AbstractTransport#sendMessage(nemos.remote.rsa.message.data.Message)
     */
    @Override
    protected void sendMessage(final Message pMessage) {
        mPeer.incomingMessage(pMessage);
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.remote.rsa.message.transport.AbstractTransport#close()
     */
    @Override
    public void close() {
        privateClose();
        mPeer.privateClose();
    }

    /**
     * The private close method.
     */
    private void privateClose() {
        super.close();
    }
}
